package servlet;

import com.alibaba.fastjson.JSON;
import entity.Product;
import service.IProductService;
import service.impl.ProductServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "spds", urlPatterns = {"/show_product_detail"})
public class ShowProductDetailServlet extends HttpServlet {
    private IProductService ps = new ProductServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        System.out.println("aa");
        String pidStr = request.getParameter("pid");//获得商品ID字符串
        //把ID字符串转为int
//        Integer pid=(Integer)pidStr;//不能强制转换

//        Integer pid=new Integer(pidStr);//使用new浪费内存空间
//        int id=pid;
        int pid = Integer.parseInt(pidStr);//推荐使用
        System.out.println(pid);
        Product product = ps.showDetail(pid);
        response.setContentType("text/json;charset=utf-8");
        String s = JSON.toJSONString(product);
        response.getWriter().write(s);
        HttpSession session = request.getSession();//获取浏览器会话
        session.setAttribute("detail", s);
//TODO 调用service，获得商品详情，作为一次性的数据，保存在请求作用域，转发到product_detail.jsp
    }


}
